package com.cskaoyan.config;

import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.stereotype.Component;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;

/**
 * @program: project-market
 * @description: 跨域时候获取相同的sessionId
 * @author: fish-team
 * @create: 2021-13-3 16:55
 **/
@Component
public class MarketSessionManager extends DefaultWebSessionManager {

    //写这个key去看前端应用的请求头
    String ADMIN_TOKEN_HEADER = "X-CskaoyanMarket-Admin-Token";
    String WX_TOKEN_HEADER = "X-CskaoyanMarket-Token";


    @Override
    protected Serializable getSessionId(ServletRequest request, ServletResponse response) {

        HttpServletRequest hsRequest = (HttpServletRequest) request;

        String adminSessionId = hsRequest.getHeader(ADMIN_TOKEN_HEADER);
        if (adminSessionId != null && !"".equals(adminSessionId)) {
            return adminSessionId;
        }

        String wxSessionId = hsRequest.getHeader(WX_TOKEN_HEADER);
        if (wxSessionId != null && !"".equals(wxSessionId)) {
            return wxSessionId;
        }

        return super.getSessionId(request, response);
    }
}
